<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户注册</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 20px;
        }

        .register-container {
            background: white;
            border-radius: 16px;
            box-shadow: 0 10px 40px rgba(0,0,0,0.3);
            padding: 40px;
            width: 100%;
            max-width: 450px;
        }

        .register-header {
            text-align: center;
            margin-bottom: 30px;
        }

        .register-header h1 {
            font-size: 28px;
            color: #333;
            margin-bottom: 10px;
        }

        .register-header p {
            color: #666;
            font-size: 14px;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            color: #333;
            font-weight: 500;
            font-size: 14px;
        }

        .form-group input,
        .form-group select {
            width: 100%;
            padding: 12px 15px;
            border: 2px solid #e0e0e0;
            border-radius: 8px;
            font-size: 14px;
            transition: all 0.3s;
        }

        .form-group input:focus,
        .form-group select:focus {
            outline: none;
            border-color: #667eea;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
        }

        .btn {
            width: 100%;
            padding: 14px;
            border: none;
            border-radius: 8px;
            font-size: 16px;
            font-weight: bold;
            cursor: pointer;
            transition: all 0.3s;
            margin-bottom: 15px;
        }

        .btn-primary {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
        }

        .btn-primary:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
        }

        .btn-secondary {
            background: white;
            color: #667eea;
            border: 2px solid #667eea;
        }

        .btn-secondary:hover {
            background: #f5f5f5;
        }

        .message {
            padding: 12px;
            border-radius: 8px;
            margin-bottom: 20px;
            display: none;
            font-size: 14px;
        }

        .message.success {
            background: #e8f5e9;
            color: #2e7d32;
            border: 1px solid #4caf50;
        }

        .message.error {
            background: #ffebee;
            color: #c62828;
            border: 1px solid #f44336;
        }

        .message.show {
            display: block;
        }

        .link {
            text-align: center;
            margin-top: 20px;
            color: #666;
            font-size: 14px;
        }

        .link a {
            color: #667eea;
            text-decoration: none;
            font-weight: 500;
        }

        .link a:hover {
            text-decoration: underline;
        }

        .required {
            color: red;
        }
    </style>
</head>
<body>
    <div class="register-container">
        <div class="register-header">
            <h1>🎉 用户注册</h1>
            <p>创建您的账号开始使用系统</p>
        </div>

        <div class="message" id="message"></div>

        <form id="registerForm">
            <div class="form-group">
                <label>用户名 <span class="required">*</span></label>
                <input type="text" id="username" name="username" placeholder="请输入用户名" required>
            </div>

            <div class="form-group">
                <label>密码 <span class="required">*</span></label>
                <input type="password" id="password" name="password" placeholder="至少6位密码" required>
            </div>

            <div class="form-group">
                <label>确认密码 <span class="required">*</span></label>
                <input type="password" id="confirmPassword" name="confirmPassword" placeholder="再次输入密码" required>
            </div>

            <div class="form-group">
                <label>用户类型 <span class="required">*</span></label>
                <select id="administrator" name="administrator" required>
                    <option value="0">普通用户</option>
                    <option value="1">管理员</option>
                </select>
            </div>

            <div class="form-group">
                <label>邮箱</label>
                <input type="email" id="email" name="email" placeholder="选填，用于找回密码">
            </div>

            <div class="form-group">
                <label>手机号</label>
                <input type="tel" id="phone" name="phone" placeholder="选填，11位手机号">
            </div>

            <button type="submit" class="btn btn-primary">立即注册</button>
            <button type="button" class="btn btn-secondary" onclick="window.location.href='login.html'">返回登录</button>
        </form>

        <div class="link">
            已有账号？<a href="login.html">立即登录</a>
        </div>
    </div>

    <script>
        const API_BASE_URL = '/api/user';

        document.getElementById('registerForm').addEventListener('submit', async (e) => {
            e.preventDefault();

            const formData = {
                username: document.getElementById('username').value.trim(),
                password: document.getElementById('password').value,
                confirmPassword: document.getElementById('confirmPassword').value,
                administrator: parseInt(document.getElementById('administrator').value),
                email: document.getElementById('email').value.trim(),
                phone: document.getElementById('phone').value.trim()
            };

            // 前端验证
            if (formData.password !== formData.confirmPassword) {
                showMessage('两次输入的密码不一致', 'error');
                return;
            }

            if (formData.password.length < 6) {
                showMessage('密码长度不能少于6位', 'error');
                return;
            }

            try {
                const response = await fetch(`${API_BASE_URL}/register`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(formData)
                });

                const result = await response.json();

                if (result.code === 200) {
                    showMessage('✅ ' + result.data.message + '，3秒后跳转到登录页面...', 'success');
                    setTimeout(() => {
                        window.location.href = 'login.html';
                    }, 3000);
                } else {
                    showMessage('❌ ' + result.message, 'error');
                }

            } catch (error) {
                console.error('注册失败:', error);
                showMessage('❌ 注册失败：' + error.message, 'error');
            }
        });

        function showMessage(text, type) {
            const messageEl = document.getElementById('message');
            messageEl.textContent = text;
            messageEl.className = `message ${type} show`;

            if (type === 'error') {
                setTimeout(() => {
                    messageEl.classList.remove('show');
                }, 5000);
            }
        }
    </script>
</body>
</html>

